var pageSize = 10;
var pageNum = 1;
var page;
var laypage;
var list;
var layer;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入
    });

    setTimeout(function () {
        fundUserList();
    }, 200);
});

//查询用户列表
function fundUserList() {
    var menuId = $("#menuId").val();
    var role = $("#role").val();
    var userStatus = $("#userStatus").val();
    var queryKey = $("#queryKey").val();

    var params = {
        "type": "query",
        "menuId": menuId,
        "pageSize": pageSize,
        "pageNum": pageNum,
        "userStatus": userStatus,
        "queryKey": queryKey,
        "role": role
    };
    $.ajax({
        type: "post",
        url: "/user/fundUserList",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data.code == 1 && data.code != undefined) {
                list = data.list;
                page = data.page;
                var html = "";
                for (var b in list) {
                    var roelStr = "";
                    if (list[b].role == "USER") {
                        roelStr = "普通用户";
                    } else if (list[b].role == "SUPER") {
                        roelStr = "官方特权用户";
                    } else if (list[b].role == "CHUANG") {
                        roelStr = "创客";
                    } else if (list[b].role == "HEHUOREN") {
                        roelStr = "合伙人";
                    } else if (list[b].role == "SERVER") {
                        roelStr = "服务商";
                    }
                    var birthday = '';
                    if (list[b].birthday != "" && list[b].birthday != null) {
                        birthday = new Date(list[b].birthday).Format("yyyy-MM-dd");
                    }
                    html += '<tr><td>' + list[b].id + '</td>' +
                        '<td>' + list[b].nickName + '</td>' +
                        '<td>' + (list[b].sex == 0 ? "男" : "女") + '</td>' +
                        '<td>' + birthday + '</td>' +
                        '<td>' + (list[b].phone != null ? list[b].phone : "") + '</td>' +
                        '<td>' + roelStr + '</td>' +
                        '<td>' + (list[b].state == 1 ? "正常" : "封停") + '</td>' +
                        '<td>' + new Date(list[b].createTime).Format("yyyy-MM-dd hh:mm:ss").toLocaleString() + '</td>' +
                        '<td><div class="visible-md visible-lg visible-sm visible-xs action-buttons"><a class="green exchange" title="查看邀请" onclick="findUserDetail(' + list[b].id + ')"><i class="glyphicon glyphicon-search">&nbsp;&nbsp;</i></a>'
                    if ("USER" == list[b].role) {
                        html += '<a class="green exchange3" title="修改邀请人" onclick="modifyInviter(' + list[b].id + ')"><i class="glyphicon glyphicon-pencil">&nbsp;&nbsp;</i></a>'
                    }
                    html += '</div></td></tr>';
                }
                $("#bus").html(html);
                $("#total").html("共有数据：" + page.total + " 条");
                laypage({
                    cont: 'page'
                    , pages: page.pages
                    , first: 1
                    , curr: pageNum
                    , last: page.pages
                    , prev: '<em><</em>'
                    , next: '<em>></em>'
                    , jump: function (obj, first) {
                        pageNum = obj.curr;
                        if (!first) {
                            fundUserList();
                        }
                    }
                });
            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
            }
        }
    });
}

//查询按钮
function search() {
    pageNum = 1;
    fundUserList();
}

//查看用户邀请
function findUserDetail(userId) {
    sessionStorage.userId = userId;
    layer.open({
        title: ['邀请', 'color: #2299ee;border-bottom: 1px solid;']
        , type: 2
        , content: 'userDetail.html'
        , area: ['100%', '100%']
        , maxmin: true
    });
}

//修改邀请人
function modifyInviter(userId) {
    sessionStorage.userId = userId;
    layer.open({
        title: ['修改邀请人', 'color: #2299ee;border-bottom: 1px solid;']
        , type: 2
        , content: 'userModifyInviter.html'
        , area: ['80%', '80%']
        , maxmin: true
    });
}
